package com.winhxd.b2c.customer.service.impl;

import com.winhxd.b2c.common.domain.customer.bo.CustomerRevenueBO;
import com.winhxd.b2c.common.domain.customer.model.CustomerIntegralDetail;
import com.winhxd.b2c.common.domain.customer.model.CustomerRevenueDetail;
import com.winhxd.b2c.common.domain.customer.model.CustomerWallet;
import com.winhxd.b2c.customer.dao.CustomerWalletMapper;
import com.winhxd.b2c.customer.service.CustomerRevenueService;
import com.winhxd.b2c.customer.service.CustomerWalletService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author caiyulong
 */
@Service
public class CustomerWalletServiceImpl implements CustomerWalletService {

    @Autowired
    private CustomerWalletMapper customerWalletMapper;

    @Autowired
    private CustomerRevenueService customerRevenueService;

    @Override
    public CustomerRevenueBO getCustomerRevenueByCustomerId(Long customerId, Integer level) {
        return customerWalletMapper.selectCustomerRevenueByCustomerId(customerId, level);
    }

    @Override
    public void modifyCustomerWallet(List<CustomerIntegralDetail> customerIntegralDetails, List<CustomerRevenueDetail> customerRevenueDetails, Long firstCustomerId) {

        List<Long> customerIdList = customerIntegralDetails.stream().map(CustomerIntegralDetail::getCustomerId).collect(Collectors.toList());
        List<CustomerWallet> customerWallets = customerWalletMapper.selectByCustomerIdList(customerIdList);
        List<CustomerWallet> insertList = new ArrayList<>();
        List<CustomerWallet> updateList = new ArrayList<>();
        customerIntegralDetails.stream().forEach(customerIntegralDetail -> {
            List<CustomerWallet> wallets = customerWallets.stream().filter(customerWallet -> customerWallet.getCustomerId().equals(customerIntegralDetail.getCustomerId())
                    && CustomerRevenueServiceImpl.getRevenueLevel(Integer.valueOf(customerWallet.getLevel())) != null
                    && CustomerRevenueServiceImpl.getRevenueLevel(Integer.valueOf(customerWallet.getLevel())).equals(customerIntegralDetail.getLevel()))
                    .collect(Collectors.toList());
            CustomerRevenueDetail revenueDetail = customerRevenueDetails.stream().filter(customerRevenueDetail -> customerRevenueDetail.getCustomerId().equals(customerIntegralDetail.getCustomerId()))
                    .collect(Collectors.toList()).get(0);
            if (CollectionUtils.isNotEmpty(wallets)){
                CustomerWallet customerWallet = wallets.get(0);
                if (Objects.nonNull(firstCustomerId) && firstCustomerId.equals(customerWallet.getCustomerId())){
                    BigDecimal lowerRevenue = Objects.isNull(customerWallet.getLowerRevenue()) ? BigDecimal.ZERO : customerWallet.getLowerRevenue();
                    customerWallet.setLowerRevenue(lowerRevenue.add(revenueDetail.getAmount()));
                }
                customerWallet.setTotalIntegral(customerWallet.getTotalIntegral() + customerIntegralDetail.getIntegral());
                customerWallet.setRemainIntegral(customerWallet.getRemainIntegral() + customerIntegralDetail.getIntegral());
                customerWallet.setTotalRevenue(customerWallet.getTotalRevenue().add(revenueDetail.getAmount()));
                customerWallet.setBalance(customerWallet.getBalance().add(revenueDetail.getAmount()));
                customerWallet.setUpdated(new Date());
                updateList.add(customerWallet);
                modifyCustomerWalletCache(customerWallet, customerIntegralDetail.getLevel());
            }else{
                CustomerWallet customerWallet = new CustomerWallet();
                if (Objects.nonNull(firstCustomerId) && firstCustomerId.equals(customerIntegralDetail.getCustomerId())){
                    customerWallet.setLowerRevenue(revenueDetail.getAmount());
                }
                customerWallet.setCustomerId(customerIntegralDetail.getCustomerId());
                customerWallet.setLevel(customerIntegralDetail.getLevel());
                customerWallet.setTotalRevenue(revenueDetail.getAmount());
                customerWallet.setBalance(revenueDetail.getAmount());
                customerWallet.setTotalIntegral(customerIntegralDetail.getIntegral());
                customerWallet.setRemainIntegral(customerIntegralDetail.getIntegral());
                customerWallet.setWithdrawNumber(0);
                customerWallet.setConsumeIntegralNumber(0);
                customerWallet.setWithdraw(BigDecimal.ZERO);
                customerWallet.setConsumedIntegral(0);
                insertList.add(customerWallet);
            }
        });
        if (CollectionUtils.isNotEmpty(insertList)){
            customerWalletMapper.batchInsert(insertList);
        }
        if (CollectionUtils.isNotEmpty(updateList)){
            customerWalletMapper.batchUpdate(updateList);
        }
    }

    private void modifyCustomerWalletCache(CustomerWallet customerWallet, Byte level) {
        CustomerRevenueBO customerRevenueBO = customerRevenueService.getCustomerRevenueCache(customerWallet.getCustomerId(), Integer.valueOf(level));
        if (Objects.nonNull(customerRevenueBO)){
            customerRevenueBO.setTotalRevenue(customerWallet.getTotalRevenue());
            customerRevenueBO.setRemainIntegral(customerWallet.getRemainIntegral());
            customerRevenueBO.setBalance(customerWallet.getBalance());
            customerRevenueBO.setLowerRevenue(customerWallet.getLowerRevenue());
            customerRevenueService.modifyCustomerRevenueCache(customerWallet.getCustomerId(), Integer.valueOf(level), customerRevenueBO);
        }
    }

}


